from google.appengine.ext import webapp
from google.appengine.api.urlfetch import fetch , GET , POST , DELETE
from com.blogspot.zizon.twitter.Oauth import quote , unquote
from com.blogspot.zizon.twitter.Oauth import Oauth
from test.test_iterlen import len

"""
    proxy apis
"""
class RestApiProxy(webapp.RequestHandler): 
    
    """
        overide this method to get proxied api server url
    """
    def getApiServer(self):
        return "";
    
    """
        pre-set http oauth headers
    """
    def prepareHeader(self, method, parameters):
        """ need authorize """
        if "Authorization" in self.request.headers and Oauth.identity() in self.request.headers["Authorization"]:
            header = Oauth.buildOauthHeader()
            parameters = parameters.split("&")
            for pairs in parameters:
                item = pairs.split("=")
                if len(item) == 2:
                    header[item[0]] = unquote(item[1])
            header["oauth_signature"] = Oauth.signingRequest(
                                                             method,
                                                             "%s%s" % (
                                                                    self.getApiServer(),
                                                                    self.request.path.replace("/twitter/", ""),
                                                                   ),
                                                             header,
                                                            Oauth.comsumer_secret,
                                                            Oauth.oauth_token_secret)
            self.request.headers["Authorization"] = "OAuth %s" % (", ".join("%s=\"%s\"" % (quote(key), quote(value)) for key, value in header.items()))
     
    """
        just forward request , replacing original host
    """
    def proxy(self , method , *args):
        """ do api proxy """
        response = fetch(
                            "%s%s?%s" % (
                                            self.getApiServer() ,
                                            self.request.path.replace("/twitter/", ""),
                                            self.request.query_string
                                        ),
                            self.request.body,
                            method,
                            self.request.headers,
                        )
        self.response.headers = response.headers
        self.response.out.write(response.content)

    """
        handle http get
    """    
    def get(self, *args):
        self.prepareHeader("GET", self.request.query_string)
        self.proxy(GET, *args)
        
    """
        handle http post
    """
    def post(self, *args):
        self.prepareHeader("POST", self.request.body)
        self.proxy(POST, *args)
    
    """
        handle http delete
    """    
    def delete(self , *arps):
        self.proxy(DELETE, *arps)
